<template>
  <div class="searchResult">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="(item, index) in list"
        :key="index"
        :title="item"
      />
    </van-list>
  </div>
</template>

<script>
import { getSearchResult } from "../api/search.js";
export default {
  name: "searchResult",
  props: {
    searchText: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 2,
      perpage: 10
    };
  },
  methods: {
    async onLoad() {
      //   请求数据
      const { res } = getSearchResult({
        page: this.page,
        per_page: this.perpage,
        q: this.searchText
      });
      console.log(res);
      // 将数据放到列表中
      const { options } = res;
      this.list.push(...options);
      // 判断是否还有数据
      this.loading = false;
      // 有就更新下一页
      if (options.length) {
        this.page++;
      } else {
        this.finished = true;
      }
    }
  }
};
</script>
<style scoped lang="less">
.searchResult {
  position: fixed;
  left: 0;
  right: 0;
  top: 54px;
  bottom: 0;
  // 能正常滑动
  overflow-y: auto;
}
</style>
